﻿Imports System.Net.Sockets
Imports System.Text
Imports System.IO

Public Class nsWebProxyClient
  Private _server As String
  Private _port As Integer
  Private _site As String
  Private _page As String
  Private _data As String
  Private _useEncryption As Boolean
  Private wpClient As New TcpClient
  Private ns As NetworkStream
  Private connected As Boolean = False
  Private _errors As New clsErrors

  Const DEFAULTSERVER As String = "mytestweb"
  Const DEFAULTPORT As Integer = 5063
  Const MAXWAITTIME As Integer = 60

  Public Property Server() As String
    Get
      Return _server
    End Get
    Set(ByVal value As String)
      _server = value
    End Set
  End Property

  Public Property Port() As Integer
    Get
      Return _port
    End Get
    Set(ByVal value As Integer)
      _port = value
    End Set
  End Property

  Public Property Site() As String
    Get
      Return _site
    End Get
    Set(ByVal value As String)
      _site = value
    End Set
  End Property

  Public Property Page() As String
    Get
      Return _page
    End Get
    Set(ByVal value As String)
      _page = value
    End Set
  End Property

  Public Property Data() As String
    Get
      Return _data
    End Get
    Set(ByVal value As String)
      _data = value
    End Set
  End Property

  Public Property UseEncryption() As Boolean
    Get
      Return _useEncryption
    End Get
    Set(ByVal value As Boolean)
      _useEncryption = value
    End Set
  End Property

  ReadOnly Property Errors() As clsErrors
    Get
      Return _errors
    End Get
  End Property

  Protected Sub setError(ByVal errNbr As Integer, ByVal errDesc As String, Optional ByVal fieldName As String = "")
    _errors.Add(New clsError(errNbr, errDesc, fieldName))
  End Sub
  Public Sub clearErrors()
    _errors.Clear()
  End Sub


  Public Sub New()
    initClass()
  End Sub

  Public Sub initClass()
    _data = ""
    _page = ""
    _site = ""
    _server = DEFAULTSERVER
    _port = DEFAULTPORT
  End Sub

  Public Function Connect() As Boolean
    Try
      wpClient.Connect(_server, _port)
      ns = wpClient.GetStream
      connected = True
    Catch ex As Exception
      setError(-1, ex.Message)
      connected = False
    End Try
    Return connected
  End Function

  Public Function SendRequest(Optional ByVal sSite As String = "", Optional ByVal sPage As String = "", _
                              Optional ByVal sData As String = "") As Boolean
    If sSite.Length > 0 Then
      _site = sSite
    End If
    If sPage.Length > 0 Then
      _page = sPage
    End If
    If sData.Length > 0 Then
      _data = sData
    End If

    If Not connected Then
      setError(-2, "Must connect before sending request.")
      Return False
    End If

    If _site.Length = 0 Then
      setError(-3, "Site not specified.")
      Return False
    End If

    If _page.Length = 0 Then
      setError(-4, "Page not specified.")
      Return False
    End If

    Try
      ' send message
      Dim requestMsg As New StringBuilder
      requestMsg.Append("<Request site=""" & _site & """ page=""" & _page & """>")
      requestMsg.Append("<Data>")
      requestMsg.Append("<![CDATA[")
      requestMsg.Append(sData)
      requestMsg.Append("]]>")
      requestMsg.Append("</Data>")
      requestMsg.Append("</Request>")
      Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes(requestMsg.ToString)
      ns.Write(outStream, 0, outStream.Length)
      ns.Flush()
      Return True
    Catch ex As Exception
      setError(-5, ex.Message)
      Return False
    End Try

  End Function

  Public Function GetResponse() As Byte()
    Dim waitTime As Long = 0
    ' receive message
    Dim readBuffer(1024) As Byte
    Dim memBuffer As New MemoryStream
    Dim readLen As Integer
    'Dim responseMsg As StringBuilder = New StringBuilder()

    ns.ReadTimeout = 60000

    ' wait till data is available
    Do Until ns.DataAvailable
      Threading.Thread.Sleep(100)
      waitTime = waitTime + 1
      If waitTime > 600 Then
        Exit Do
      End If
    Loop
    Do
      readLen = ns.Read(readBuffer, 0, 1024)
      memBuffer.Write(readBuffer, 0, readLen)
      'responseMsg.Append(Encoding.ASCII.GetString(readBuffer, 0, readLen))
    Loop While ns.DataAvailable
    Return memBuffer.GetBuffer()
  End Function

End Class
